package com.ffapp.rainmonitorcustom.alarm;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.basis.BasisActivity;
import com.ffapp.rainmonitorcustom.basis.BasisApp;
import com.ffapp.rainmonitorcustom.basis.Constants;
import com.ffapp.rainmonitorcustom.bean.BaseListBeanYL;
import com.ffapp.rainmonitorcustom.bean.DeviceListBean;
import com.ffapp.rainmonitorcustom.bean.DeviceListData;
import com.ffapp.rainmonitorcustom.util.MyUtils;
import com.ffapp.rainmonitorcustom.video.VideoInfoActivity;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.loopj.android.http.RequestParams;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

/**
 * 巡警搜索
 */
public class AlarmSearchActivity extends BasisActivity {

    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;

    private MyAdapterList mAdapter;
    private DeviceListBean mListBean;

    @BindView(R.id.edit_search)
    EditText edit_search;
    @BindView(R.id.refresh_text_empty)
    TextView refresh_text_empty;
    String searchkey;


    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.alarm_search_activity);

        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        mRecyclerView.setVerticalScrollBarEnabled(true);
        mRecyclerView.setLayoutManager(layoutManager);
//        mRecyclerView.setEmptyView(findViewById(R.id.refresh_view));
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mListBean.refresh();
                getList();
            }

            @Override
            public void onLoadMore() {
                getList();
            }
        });
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadingMoreEnabled(true);

        edit_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if ((actionId == 0 || actionId == 3) && event != null) {
                    search();
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mListBean = new DeviceListBean();
        setListView();
        getList();
    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    private void onListViewComplete() {
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();
        mRecyclerView.setLoadingMoreEnabled(mListBean.hasNextPage());
//        mRecyclerView.setLoadingMoreEnabled(false);
        setEmptyView();
    }

    private void setEmptyView() {
        if (mListBean == null || mListBean.getList().size() == 0) {
            findViewById(R.id.refresh_view).setVisibility(View.VISIBLE);
            findViewById(R.id.refresh_view).setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            mRecyclerView.refresh();
                        }
                    });

            refresh_text_empty.setText("未找到您查询“" + searchkey + "”的相关信息");
        } else {
            findViewById(R.id.refresh_view).setVisibility(View.INVISIBLE);
        }
    }

    private void getList() {
        RequestParams params = new RequestParams();
        showProgress();
        params.put("keyword", searchkey);
        params.put(BaseListBeanYL.PAGE_NAME, mListBean.getNextPage());
        params.put(BaseListBeanYL.PAGE_SIZE_NAME, BaseListBeanYL.PAGE_SIZE);
        HttpRestClient.get(Constants.URL_IMG_LIST, params, myHttpListener,
                HTTP_LIST, DeviceListBean.class);

    }

    private final static int HTTP_LIST = 11;
    MyHttpListener myHttpListener = new MyHttpListener() {
        @Override
        public void onSuccess(int httpWhat, Object result) {
            DeviceListBean listBean = (DeviceListBean) result;
            mListBean = listBean;
            setListView();
        }

        @Override
        public void onFailure(int httpWhat, Object result) {
            super.onFailure(httpWhat, result);
//            mListBean = new ServiceNewListBean();
            setListView();
        }

        @Override
        public void onFinish(int httpWhat) {
            hideLoading();
            onListViewComplete();
        }
    };


    @OnClick(R.id.img_search)
    void search() {
        searchkey = edit_search.getText().toString().trim();
        mListBean.refresh();
        getList();
    }

    @OnClick(R.id.img_cancel)
    void searchCancel() {
        edit_search.setText("");
    }

    @OnClick(R.id.text_search)
    void canel() {
       search();
    }

    @OnClick(R.id.base_btn_back)
    void canelIcon() {
        finish();
    }


    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public MyAdapterList.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.video_list_item, viewGroup, false);
            return new MyAdapterList.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(MyAdapterList.ViewHolder viewHolder, int position) {

            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.img_photo)
            public ImageView img_photo;
            @BindView(R.id.tv_name)
            public TextView mTvName;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                DeviceListData listData = mListBean.getList().get(position);
                mTvName.setText(listData.getStnm());
//                BasisApp.loadImg(mContext, Constants.URL_TEST_API + listData.getImgurl(), img_photo, R.drawable.video_img_bg_default);
                BasisApp.loadImg(mContext, MyUtils.getServiceAPI() + listData.getImgurl(), img_photo, R.drawable.video_img_bg_default);
                list_item.setTag(position);
            }


            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                int position = (int) v.getTag();
                DeviceListData deviceListData = mListBean.getList().get(position);
                Bundle extras = new Bundle();
                extras.putSerializable("bg", deviceListData.getImgurl());
                extras.putSerializable("stcd", deviceListData.getStcd());
                extras.putSerializable("title", deviceListData.getStnm());
                ActivityTool.skipActivity(mContext, VideoInfoActivity.class, extras);
            }
        }

    }

}

